﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public partial class BattleCtr : XObjBase
{
    public static BattleCtr Instance;
    private DataBattleMgr _dataBattleMgr;
    public DataBattleMgr dataBattleMgr
    {
        get
        {
            return _dataBattleMgr;
        }
    } 

    protected override void Awake()
    {
        base.Awake();
        Instance = this;
        this.OnInitRender();
    }
    protected override void OnEnable()
    {
        base.OnEnable();
        //强制退出战斗
        Facade.Instance.RegistNetEvent(nameof(notify_battlefinish_rsp), OnHandleNotifyBattlefinishRsp);

        //有人退出战斗
        Facade.Instance.RegistNetEvent(nameof(notify_exitroom_rsp), OnHandleNotifyExitbattleRsp);

        //有玩家做操作
        Facade.Instance.RegistNetEvent(nameof(notify_action_rsp), OnHandleNotifyAction);

        //有玩家断网重连进来了
        Facade.Instance.RegistNetEvent(nameof(notify_enterroom_rsp), OnHandleNotifyEnterroomRsp);//php

        Facade.Instance.RegistNetEvent(nameof(notify_syncbattle_rsp), OnHandleNotifySyncBattleRsp);
        
    }

    protected override void OnDisable()
    {
        base.OnDisable();
        Facade.Instance.UnRegistNetEvent(nameof(notify_battlefinish_rsp), OnHandleNotifyBattlefinishRsp);
        Facade.Instance.UnRegistNetEvent(nameof(notify_exitroom_rsp), OnHandleNotifyExitbattleRsp);//php
        Facade.Instance.UnRegistNetEvent(nameof(notify_action_rsp), OnHandleNotifyAction);//php
        Facade.Instance.UnRegistNetEvent(nameof(notify_enterroom_rsp), OnHandleNotifyEnterroomRsp);
        Facade.Instance.UnRegistNetEvent(nameof(notify_syncbattle_rsp), OnHandleNotifySyncBattleRsp);
    }

    public void OnHandleData(DataBattleMgr data)
    {
        Debug.Log("SceneBattle onahdnle data:" + data);
        _dataBattleMgr = data;
        StartCoroutine(CoWaitUIBattle());
    }

    //数据处理中心
    private void OnHandleNotifyAction(System.Object data)
    {
        var rsp = data as notify_action_rsp;
        DataBattlePlayer dataBattlePlayer;
        switch (rsp.type)
        {
            case ActionType.Close:
                dataBattlePlayer = _dataBattleMgr.GetDataPlayer(rsp.touid);
                dataBattlePlayer.SetState(BattlePlayerState.Close, rsp.fromuid, null);
                ShowAction(rsp.type, rsp.fromuid, rsp.touid);
                break;
            case ActionType.Shake:
                dataBattlePlayer = _dataBattleMgr.GetDataPlayer(rsp.touid);
                dataBattlePlayer.SetState(BattlePlayerState.Shake, rsp.fromuid, rsp.content);
                ShowAction(rsp.type, rsp.fromuid, rsp.touid);
                break;
            case ActionType.Open:
                dataBattlePlayer = _dataBattleMgr.GetDataPlayer(rsp.touid);
                dataBattlePlayer.SetState(BattlePlayerState.Open, rsp.fromuid, null);
                ShowAction(rsp.type, rsp.fromuid, rsp.touid);
                break;
            case ActionType.Glance:
                dataBattlePlayer = _dataBattleMgr.GetDataPlayer(rsp.touid);
                dataBattlePlayer.SetState(BattlePlayerState.Glance, rsp.fromuid, null);
                ShowAction(rsp.type, rsp.fromuid, rsp.touid);
                break;
            case ActionType.LookByMySelf:
                dataBattlePlayer = _dataBattleMgr.GetDataPlayer(rsp.touid);
                dataBattlePlayer.SetState(BattlePlayerState.LookByMySelf, rsp.fromuid, null);
                ShowAction(rsp.type, rsp.fromuid, rsp.touid);
                break;
            case ActionType.Challenge:
                dataBattlePlayer = _dataBattleMgr.GetDataPlayer(rsp.fromuid);
                dataBattlePlayer.SetChallenge(rsp.content);
                break;
            case ActionType.Chat:
                dataBattlePlayer = _dataBattleMgr.GetDataPlayer(rsp.fromuid);
                dataBattlePlayer.SetChat(rsp.content);
                break;
            default:
                Debug.LogError("can not handle type:" + rsp.type);
                break;
        }
        Facade.Instance.SendEvent(EventID.ActionBattleReceive, rsp);
    }

    private void OnHandleNotifyEnterroomRsp(System.Object data)
    {
        var rsp = data as notify_enterroom_rsp;

        var dataPlayer = _dataBattleMgr?.GetDataPlayer(rsp.uid);
        if (dataPlayer != null)
        {
            dataPlayer.SetIsOff("online");
        }
        UITips.Instance.Show(XLocalization.Get("有人重新回来了"));
    }

    private void OnHandleNotifySyncBattleRsp(System.Object data)
    {
        var rsp = data as notify_syncbattle_rsp;

        var persons = rsp.persons;
        foreach (var person in persons)
        {
            var dataBattlePlayer = _dataBattleMgr.GetDataPlayer(person.uid);
            dataBattlePlayer.SetState((BattlePlayerState)person.state, person.uid, person.content);
        }

        //统计骰子
        Facade.Instance.SendEvent(EventID.BattlePlayerStateChange, null);
    }

    private void OnHandleNotifyBattlefinishRsp(System.Object data)
    {
        var rsp = data as notify_battlefinish_rsp;
        UITips.Instance.Show(XLocalization.Get("服务器错误，强制退出"));
        SceneMgr.Instance.LoadScene(SceneName.SceneMain);
    }

    private void OnHandleNotifyExitbattleRsp(System.Object data)
    {
        var rsp = data as notify_exitroom_rsp;

        var dataPlayer = _dataBattleMgr.GetDataPlayer(rsp.uid);
        if (dataPlayer != null)
        {
            dataPlayer.SetIsOff(rsp.reason);
        }
        UITips.Instance.Show(XLocalization.Get("有人退出战斗"));
    }
}